package main

import (
	"context"
	"github.com/robfig/cron/v3"
	"os/exec"
	"time"
)

type Task struct {
	Id     int    // Id
	Status int  // 0 需要执行 1 执行中 2 暂停
	Spec   string
	Command    string
	Schedule cron.Schedule
	NextTime time.Time
	Ctx context.Context
	Cancel context.CancelFunc
}

// 执行命令，捕获输出，kill进程
func (task *Task) Run() {
	go func() {
		if task.Status != 10 {
			return
		}

		task.Status = 1
		task.Ctx, task.Cancel = context.WithCancel(context.TODO())
		cmd := exec.CommandContext(task.Ctx, "D:\\cygwin64\\bin\\bash.exe", "-c", task.Command)
		task.Status = 0

		output, err := cmd.CombinedOutput()
		out := string(output)
		resultChan <- &Result {out, err}
	}()
}
